package fr.awtl.framework;

import java.io.IOException; 
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.xml.sax.SAXException;

import fr.awtl.framework.actions.Action;
import fr.awtl.framework.actions.forms.ActionForm;
import fr.awtl.framework.exception.ActionNotFoundException;
import fr.awtl.framework.exception.PopulateException;

public class ActionServlet extends HttpServlet {

	/**
     * 
     */
	private static final long serialVersionUID = 1L;

	@Override
	public void doGet(HttpServletRequest paramReq, HttpServletResponse paramResp)
			throws ServletException, IOException {

		String pattern = paramReq.getServletPath();

		ActionPackage pack = null;
		ActionForm actionForm = null;
		try {
			pack = callPackage(pattern);
			actionForm = (ActionForm) Class.forName(
					pack.getFormClass()).newInstance();

			Map<String, String[]> params = paramReq.getParameterMap();

			populateBean(actionForm,params);


				boolean result = actionForm.validate();
				if (result) {

					String response = ((Action) Class.forName(
							pack.getActionName()).newInstance()).execute(
							actionForm, paramReq, paramResp);

					if (response != null) {
						paramResp.sendRedirect(paramReq.getContextPath()
								+ response);
					} else {
						paramResp.sendRedirect(paramReq.getContextPath());
					}

				} else {
					System.out.println("une trace de mon cul");
					paramReq.setAttribute(pack.getFormName(), actionForm);
					paramReq.getRequestDispatcher(pack.getInput()).forward(paramReq, paramResp);
					System.out.println("formname : "+pack.getFormName() + " actionform : "+actionForm);
				}
			
		} catch (ActionNotFoundException e1) {
			e1.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (PopulateException e) {
			e.printStackTrace();
			paramReq.setAttribute(pack.getFormName(), actionForm);
			paramReq.getRequestDispatcher(pack.getInput()).forward(paramReq, paramResp);
		}

	}

	@Override
	protected void doPost(HttpServletRequest paramReq,
			HttpServletResponse paramResp) throws ServletException, IOException {
		doGet(paramReq, paramResp);
	}

	private ActionPackage callPackage(String pattern) throws ActionNotFoundException {

		ClassLoader cl = Thread.currentThread().getContextClassLoader();

		try {
			Factory.getInstance().fillMap(
					cl.getResource("resources/actions.xml").toString());
		} catch (DOMException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		ActionPackage pack = Factory.getInstance().getActionPackage(pattern);

		if (pack == null) {
			throw new ActionNotFoundException("ActionServlet : " + pattern
					+ "  not ok, pack null");
		}

		return pack;
	}

	private HashMap<String, String[]> populateBean(ActionForm actionForm,
			Map<String, String[]> params) throws PopulateException{

		return (new ActionFormFiller()).populateActionForm(actionForm, params);

	}

}
